#!/bin/usr/env python
# -*- coding: utf-8 -*-

from xml.dom.minidom import parse
import codecs 

class HadoopSiteParser:
    """ 
    hadoop-site.xml parser.
    """ 
    def __init__(self, site_file):
        self.site_file = site_file
        self.doc = parse(self.site_file)

    def _get_text(self, node):
        rc = ''
        for n in node.childNodes:
            if n.nodeType == n.TEXT_NODE:
                rc = rc + n.data
        return rc

    def _get_node(self, key):
        property_element = None
        name_element     = None
        value_element    = None

        for e in self.doc.getElementsByTagName('property'):
            for node in e.childNodes:
                if node.nodeType == node.ELEMENT_NODE:
                    if node.nodeName == 'name' and self._get_text(node) == key:
                        name_element = node
                    elif node.nodeName == 'value' and name_element:
                        value_element = node
            if name_element: 
                property_element = e
                break
        return property_element, name_element, value_element

    def _add_property(self, key, value):
        property_element = self.doc.createElement('property')
        name_element     = self.doc.createElement('name')
        name_text_node   = self.doc.createTextNode(key)
        name_element.appendChild(name_text_node)

        value_element    = self.doc.createElement('value')
        value_text_node  = self.doc.createTextNode(value)
        value_element.appendChild(value_text_node)

        property_element.appendChild(name_element)
        property_element.appendChild(value_element)

        self.doc.documentElement.appendChild(property_element)

    def _add_value(self, property_element, value):
        value_element    = self.doc.createElement('value')
        value_text_node  = self.doc.createTextNode(value)
        property_element.appendChild(value_element)

    def get(self, key):
        property_element, name_element, value_element = self._get_node(key)
        if value_element:
            return self._get_text(value_element)
        return ''

    def set(self, key, value, save=True):
        property_element, name_element, value_element = self._get_node(key)
        if value_element:
            newChild = self.doc.createTextNode(value)
            value_element.replaceChild(newChild, value_element.firstChild)
        else:
            if not name_element:
                self._add_property(key, value)
            else:
                self._add_value(property_element, value)
        if save:
            self.save()

    def save(self):
        f = open(self.site_file, 'w')
        writer = codecs.lookup('utf-8')[3](f)
        self.doc.writexml(writer, encoding='utf-8')

#class HdfsSiteParser(HadoopSiteParser):
#	"""
#	hdfs-site.xml parser
#	"""
#	def __init__()

class HadoopEnvParser:
    """
    hadoop-env.sh parser
    """
    pass

if __name__ == '__main__':
    """
    self-test
    """
    site_file   = '/share/home/testhdp/hadoop-default.xml'
    parser = HadoopSiteParser(site_file)
    parser.set('hadoop.tmp.dir', '/tmp/test')
